#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <arpa/inet.h>

// 检查系统字节序
const char* get_endianness() {
    uint32_t test = 0x12345678;
    uint8_t* bytes = (uint8_t*)&test;
    return (bytes[0] == 0x12) ? "Big Endian" : "Little Endian";
}

// 序列化32位long到字节数组（使用大端序）
void serialize_long32_big_endian(int32_t value, uint8_t* buffer) {
    uint32_t net_value = htonl((uint32_t)value);
    memcpy(buffer, &net_value, sizeof(net_value));
}

// 从字节数组反序列化32位long（使用大端序）
int32_t deserialize_long32_big_endian(const uint8_t* buffer) {
    uint32_t net_value;
    memcpy(&net_value, buffer, sizeof(net_value));
    return (int32_t)ntohl(net_value);
}

// 打印字节数组
void print_bytes(const uint8_t* buffer, size_t size) {
    for (size_t i = 0; i < size; i++) {
        printf("%02x ", buffer[i]);
    }
}

// 验证long是否为32位
void check_long_size() {
    if (sizeof(long) != 4) {
        printf("警告: long类型大小为%zu字节，不是预期的4字节\n", sizeof(long));
        printf("这可能会影响序列化和反序列化的正确性\n\n");
    } else {
        printf("long类型大小为4字节，符合预期\n\n");
    }
}

int main() {
    check_long_size();
    printf("系统字节序: %s\n", get_endianness());
    
    // 测试值（32位范围内）
    int32_t test_values[] = {
        0,
        1,
        -1,
        123456789,
        -123456789,
        2147483647,  // 2^31-1 (最大32位有符号正整数)
        -2147483648, // -2^31 (最小32位有符号负整数)
        0x12345678,
        0xABCDEF00,
        0x7FFFFFFF,
        0x80000000
    };
    
    size_t num_values = sizeof(test_values) / sizeof(test_values[0]);
    
    printf("32位long大端序序列化与反序列化测试:\n");
    printf("========================================\n\n");
    
    for (size_t i = 0; i < num_values; i++) {
        int32_t original = test_values[i];
        uint8_t buffer[4]; // 固定4字节缓冲区
        
        printf("测试值: %d (0x%08x)\n", original, (unsigned int)original);
        
        // 大端序序列化
        serialize_long32_big_endian(original, buffer);
        printf("大端序序列化: ");
        print_bytes(buffer, 4);
        printf("\n");
        
        // 大端序反序列化
        int32_t result = deserialize_long32_big_endian(buffer);
        printf("大端序反序列化: %d (0x%08x)\n", result, (unsigned int)result);
        
        // 验证结果
        if (original == result) {
            printf("✓ 验证成功\n");
        } else {
            printf("✗ 验证失败\n");
        }
        
        printf("\n");
    }
    
    // 演示跨平台数据交换
    printf("跨平台数据交换演示:\n");
    printf("========================================\n\n");
    
    int32_t data_to_send = 0x12345678;
    printf("要发送的数据: 0x%08x (%d)\n", 
           (unsigned int)data_to_send, data_to_send);
    
    // 序列化（发送端）
    uint8_t network_buffer[4];
    serialize_long32_big_endian(data_to_send, network_buffer);
    
    printf("网络字节序数据: ");
    print_bytes(network_buffer, 4);
    printf("\n");
    
    // 反序列化（接收端）
    int32_t received_data = deserialize_long32_big_endian(network_buffer);
    printf("接收到的数据: 0x%08x (%d)\n", 
           (unsigned int)received_data, received_data);
    
    if (data_to_send == received_data) {
        printf("✓ 跨平台数据交换成功\n");
    } else {
        printf("✗ 跨平台数据交换失败\n");
    }
    
    // 演示大端序的特点
    printf("\n大端序特点演示:\n");
    printf("========================================\n\n");
    
    int32_t demo_value = 0x12345678;
    uint8_t demo_buffer[4];
    
    serialize_long32_big_endian(demo_value, demo_buffer);
    
    printf("数值: 0x%08x\n", (unsigned int)demo_value);
    printf("大端序表示: ");
    print_bytes(demo_buffer, 4);
    printf("\n");
    
    printf("最高有效字节(MSB): 0x%02x (位于最低内存地址)\n", demo_buffer[0]);
    printf("最低有效字节(LSB): 0x%02x (位于最高内存地址)\n", demo_buffer[3]);
    
    return 0;
}